package com.thesis.manage.service.impl;

import com.thesis.manage.common.constant.YesOrNoEnum;
import com.thesis.manage.common.service.impl.BaseServiceImpl;
import com.thesis.manage.domain.TeacherRole;
import com.thesis.manage.mapper.TeacherRoleMapper;
import com.thesis.manage.service.TeacherRoleService;
import com.thesis.manage.service.constant.ErrorMessage;
import com.thesis.manage.service.model.RoleEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import tk.mybatis.mapper.common.Mapper;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author php
 * @date 2018/2/7
 */
@Service
public class TeacherRoleServiceImpl extends BaseServiceImpl<TeacherRole> implements TeacherRoleService {
    @Resource
    private TeacherRoleMapper teacherRoleMapper;

    @Override
    protected Mapper<TeacherRole> getMapper() {
        return teacherRoleMapper;
    }

    @Override
    protected Class<TeacherRole> getEntityClass() {
        return TeacherRole.class;
    }

    @Override
    public List<TeacherRole> findByTeacherId(Long teacherId) {
        Assert.notNull(teacherId, ErrorMessage.PARAMS_IS_NULL);
        TeacherRole role = new TeacherRole();
        role.setTeacherId(teacherId);
        role.setDelStatus(YesOrNoEnum.NO.getValue());
        return this.teacherRoleMapper.select(role);
    }

    @Override
    public List<Integer> findRolesByTeacherId(Long teacherId) {
        List<TeacherRole> teacherRoles = this.findByTeacherId(teacherId);
        List<Integer> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(teacherRoles)) {
            for (TeacherRole teacherRole : teacherRoles) {
                list.add(teacherRole.getRoleId().intValue());
            }
        }
        return list;
    }

    @Override
    public String findRoles(Long teacherId) {
        List<Integer> rolesByTeacherId = this.findRolesByTeacherId(teacherId);
        StringBuilder stringBuilder = new StringBuilder();
        if (CollectionUtils.isNotEmpty(rolesByTeacherId)) {
            for (int i = 0; i < rolesByTeacherId.size(); i++) {
                String name = RoleEnum.getName(rolesByTeacherId.get(i));
                stringBuilder.append(name);
                if (i != rolesByTeacherId.size() - 1) {
                    stringBuilder.append(",");
                }
            }
        }
        return stringBuilder.toString();
    }

    @Override
    public List<TeacherRole> list(TeacherRole teacherRole) {
        teacherRole.setDelStatus(YesOrNoEnum.NO.getValue());
        return this.teacherRoleMapper.select(teacherRole);
    }


}
